const app = getApp()
const $api = require('../../../utils/api.js').API;

Page({
    data: {
        list:{},
        imgUrl:app.globalData.imgUrl,
        cate:{},
        cateId:1,
        index:0,
        arr:[],
    },
    onLoad: function() {
        this.getlist();
    },
    getlist:function(){
        var data = {'uid':wx.getStorageSync('uid')}
        var that = this
        $api.get('cart/list',data).then(res => {
            //请求成功
            if(res.data.code == 1){
                that.setData({
                    list:res.data.data
                })
            }
        })
    },
    goinfo:function(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url:'/pages/pointsMall/info?id='+id
        })
    },
    infoDelete:function(e){
        var id = e.currentTarget.dataset.id
        var that = this
        var data = {'id':id}
        $api.delete('cart/delete',data).then(res => {
            //请求成功
            wx.showToast({
                title:res.data.msg,
                duration:3000,
                mask:true, 
                icon:'none',
            })
            if(res.data.code == 1){
                that.getlist();
            }
        }).catch(err => {
            //请求失败
            //console.log('job','请求失败');
        })
    },
    bindAcceptRule:function(e){
        var id = e.currentTarget.dataset.id
        var num = e.currentTarget.dataset.num
        var arr = this.data.arr
        var that = this
        for(var x in arr){
            if(arr[x]['id'] == id){
                arr.splice(x,1);
                that.setData({
                    arr: arr
                })
                return;
            }
        }
        var arr = {'id':id,'num':num}
        this.setData({
            arr: this.data.arr.concat(arr)
        })
    },
    payquery:function(){
        //var str = JSON.stringify(this.data.arr);
        var str = ''
        var num = ''
        for(var x in this.data.arr){
            if(str == ''){
                str = this.data.arr[x]['id']
                num = this.data.arr[x]['num']
            }else{
                str = str + ',' + this.data.arr[x]['id']
                num = num + ',' + this.data.arr[x]['num']
            }
        }
        wx.navigateTo({
            url:'/pages/pointsMall/order?str='+str+'&num='+num
        })
    },
    onShareAppMessage: function(){
        return {
            title: "购物车-天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
    }
});